package com.yurun.loginmodule.service.Impl;

import com.yurun.commonapi.api.CommonResult;
import com.yurun.commonapi.pojo.*;
import com.yurun.loginmodule.mapper.AdminMapper;
import com.yurun.loginmodule.mapper.MenuMapper;
import com.yurun.loginmodule.mapper.PermissionMapper;
import com.yurun.loginmodule.mapper.RoleMapper;
import com.yurun.loginmodule.service.UmsAdminService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.connection.BitFieldSubCommands;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * UmsAdminService实现类
 * Created by macro on 2018/4/26.
 */
@Service
public class UmsAdminServiceImpl implements UmsAdminService {
    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public Employee getAdminByUsername(BigInteger employeeId) {
        Employee employee = adminMapper.selectByName(employeeId);
        if (employee != null) {
            return employee;
        }
        return null;
    }

    @Override
    public List<Menu> getMenu(BigInteger employeeId) {
        // 获取用户角色
        int roleId = roleMapper.getRoleList(employeeId).get(0);
        // 获取所有菜单id
        List<Integer> list = menuMapper.getMenuListByRole(roleId);
        List<Menu> menusList = new ArrayList<>();
        for (int menu : list) { // 根据menuId获取菜单信息
            menusList.add(menuMapper.getMenuList(menu).get(0));
        }
        return menusList;
    }

    @Override
    public Employee getOrgIdAndBranchId(BigInteger employeeId) {
        return adminMapper.getOrgIdAndBranchId(employeeId);
    }

    @Override
    public CommonResult resetPwd(Employee employee) {
        employee.setUpdateTime(new Date());
        return CommonResult.success(adminMapper.resetPwd(employee));
    }

    @Override
    public CommonResult editEmployeeInfo(Employee employee) {
        employee.setUpdateTime(new Date());
        return CommonResult.success(adminMapper.editEmployeeInfo(employee));
    }
}
